<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../../themes/icon.css">
    <script type="text/javascript" src="../../jquery.min.js"></script>
    <script type="text/javascript" src="../../jquery.easyui.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var arrays = [];
            var goodsCateServerDatas = [
//                {
//                    id: 1,
//                    text: "分类1",
//                    children: [
//                        {
//                            id: 2,
//                            text: "分类2"
//                        }, {
//                            id: 3,
//                            text: "分类2"
//                        }
//                    ]
//                }
            ];
            var goodsBrandServerDatas = [
//                {
//                    id: 1,
//                    text: "品牌1"
//                }
            ];

            $.getJSON("http://localhost:8080/loadGoodsCateVos.htm", function (data) {
                $.each(data, function (i, n) {
                    getArray(n);
                });
//                console.log(arrays);
                $.each(getChildren(0), function (i, obj) {
                    var node = {
                        id: obj.id,
                        text: obj.name,
                        state: "closed",
                        attributes: {
                            pId: obj.pId,
                            type: obj.type,
                            rate: obj.rate,
                            level: 0
                        },
                        children: []
                    };
                    buildTree(node);
                    goodsCateServerDatas.push(node);
                });
//                console.log(goodsCateServerDatas);
                $("#goodsCateTree").combotree({//组件赋值
                    data: goodsCateServerDatas
                });
            });

            $.getJSON("http://localhost:8080/queryallbrand.htm", function (data) {
                $.each(data, function (i, n) {
                    goodsBrandServerDatas.push({
                        id: n.brandId,
                        text: n.brandName
                    });
                });
                $("#goodsBrandCombobox").combobox({
                    data: goodsBrandServerDatas,
                    valueField: 'id',
                    textField: 'text'
                });
            });

            function buildTree(node) {
                var childrenObjs = getChildren(node.id);
                if (childrenObjs && childrenObjs.length > 0) {
                    $.each(childrenObjs, function (i, n) {
                        var temp = {
                            id: n.id,
                            text: n.name,
                            state: "closed",
                            attributes: {
                                pId: n.pId,
                                type: n.type,
                                rate: n.rate,
                                level: node.attributes.level + 1
                            },
                            children: []
                        };
                        node.children.push(temp);
                        buildTree(temp);
                    });
                } else {//如果节点下面没有子节点集合了，就去掉空集合和状态字段
                    delete node.children;
                    delete node.state;
                }
            }

            function getArray(goodsCate) {
                if (goodsCate) {
                    arrays.push({
                        id: goodsCate.catId,
                        name: goodsCate.catName,
                        pId: goodsCate.catParentId,
                        type: goodsCate.typeId,
                        grade: goodsCate.catGrade,
                        rate: goodsCate.catRate
                    });
                }
                var childrens = goodsCate.cateVos;
                if (childrens && childrens.length > 0) {
                    $.each(childrens, function (i, n) {
                        getArray(n);
                    });
                }
            }

            function getChildren(pId) {
                var childrens = new Array();
                $.each(arrays, function (i, n) {
                    if (n.pId == pId) {
                        childrens.push(n);
                    }
                });
                return childrens;
            }

            $('#goodsDataGrid').datagrid({
                border: false,
                fit: true,
                rownumbers: true,
                singleSelect: false,
                ctrlSelect:true,
                url: 'http://localhost:8080/searchGoods.htm',
                method: 'get',
                toolbar: '#tb',
                footer: '#ft',
                loadMsg: '加载中...',
                emptyMsg: '暂无数据',
                queryParams: {
                    showFlag: "1",
//                    thirdId: '0'
                },
                pagination: true,
                pageNumber: 1,
                pageSize: 15,
                pageList: [5, 10, 15, 20, 50, 100],
                columns: [[
                    {field: 'goodsId', title: 'ID', hidden: true, width: 100},
                    {field: 'goodsName', title: '商品名称', hidden: true, width: 100},
                    {field: 'goodsPrice', title: '商品售价', hidden: true, width: 100},
                    {field: 'goodsNo', title: '商品编码', hidden: true, width: 100},
                    {field: 'goodsImg', title: '商品图片', hidden: true, width: 100},
                    {field: 'goodsInfo', fixed: true, title: '商品信息', width: 600, formatter: goodsInfoFormatter},
                    {field: 'addState', fixed: true, title: '是否上下架', width: 100, formatter: goodsAddStateFormatter},
                    {field: 'cate', fixed: true, title: '分类', width: 100, formatter: goodsCateFormatter},
                    {field: 'brand', fixed: true, title: '品牌', width: 100, formatter: goodsBrandFormatter}
                ]],
                loadFilter: function (data) {
                    var data = {
                        rows:data.pageBean.list,
                        total:data.pageBean.rows
                    };
                    return data;
                },
                onBeforeLoad: function (pm) {//替换参数名字
                    var pageNo = pm.page;
                    var pageSize = pm.rows;
                    pm["pageNo"] = pageNo;
                    pm["pageSize"] = pageSize;
                    delete pm.page;
                    delete pm.rows;
                }
            });

            function goodsInfoFormatter(value, row, index) {
                var html = "<img height='60' src='" + row.goodsImg + "'/>" + row.goodsName;
                return html;
            }

            function goodsAddStateFormatter(value, row, index) {
                return row.goodsAdded == '1' ? '上架' : '下架';
            }

            function goodsCateFormatter(value, row, index) {
                return row.goodsCate.catName;
            }

            function goodsBrandFormatter(value, row, index) {
                return row.goodsBrand.brandName;
            }

            $("#btn_search").click(function () {
//                alert('ss');
                var goodsTitle = $("#goodsTitle").textbox('getValue');
                var goodsNo = $("#goodsNo").textbox('getValue');
                var goodsAddState = $("#goodsAddStateCombobox").combobox("getValue");
                var goodsBrandId = $("#goodsBrandCombobox").combobox("getValue");

                var tree = $("#goodsCateTree").combotree("tree");
                var node = tree.tree("getSelected");
//                console.log(node);

                var searchParams = {
                    goodsName: goodsTitle,
                    goodsNo: goodsNo,
                    status: goodsAddState,
//                    thirdId: '0',
                    goodsBrandId: goodsBrandId,
                    showFlag: "1"
                };
                if (node) {
                    if (node.attributes.level == 0) {
                        searchParams["oneCateId"] = node.id;
                    }
                    if (node.attributes.level == 1) {
                        searchParams["twoCateId"] = node.id;
                    }
                    if (node.attributes.level == 2) {
                        searchParams["goodsCateId"] = node.id;
                    }
                }
//                console.log(searchParams);
                $("#goodsDataGrid").datagrid('reload', searchParams);
            });
        });
    </script>
</head>
<body>
<table id="goodsDataGrid"></table>
<div id="tb" style="padding:2px 5px;">
    商品标题: <input class="easyui-textbox" id="goodsTitle" style="width:110px">
    商品编号: <input class="easyui-textbox" id="goodsNo" style="width:110px">
    是否上架:
    <select class="easyui-combobox" id="goodsAddStateCombobox" panelHeight="auto" style="width:100px">
        <option value="1">上架</option>
        <option value="0">下架</option>
    </select>
    分类:
    <input class="easyui-combotree" panelHeight="auto" style="width: 200px;" id="goodsCateTree">
    品牌:
    <select class="easyui-combobox" panelHeight="auto" style="width:100px" id="goodsBrandCombobox"></select>
    <a href="#" class="easyui-linkbutton" id="btn_search" iconCls="icon-search">搜索</a>
    <br/>
    <a href="#" class="easyui-linkbutton" plain="true">删除</a>
    <a href="#" class="easyui-linkbutton" plain="true">新增</a>
    <a href="#" class="easyui-linkbutton" plain="true">修改</a>
    <a href="#" class="easyui-linkbutton" plain="true">批量上架</a>
    <a href="#" class="easyui-linkbutton" plain="true">批量下架</a>
</div>
</body>
</html>